package com.frxuexi.shop.viewmodel

import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.http.ShopApiService
import com.frxuexi.lib_base.mvi.BaseViewModel
import com.frxuexi.shop.ShopDataSource
import com.frxuexi.shop.intent.ShopIntent
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class ShopViewModel @Inject constructor(val apiService: ShopApiService):BaseViewModel<ShopIntent>(){
    override fun handleIntent(it: ShopIntent) {
        when(it){
            is ShopIntent.getHomeData->httpRequest(apiService.getHomeData())
//            is ShopIntent.getShops->httpRequest(apiService.getShops(it.type,it.page),RequestType.SHOPS)
            is ShopIntent.getShops->pagerRequest ({
                ShopDataSource(apiService,it.type)
            },RequestType.SHOPS)
            is ShopIntent.getShopsByKey->httpRequest(apiService.getShopsByKey(it.key),RequestType.SELECT_KEY)
            is ShopIntent.getShopById->httpRequest(apiService.getShopById(it.id),RequestType.SELECT_ITEM)
            is ShopIntent.addCart->httpRequest(apiService.addCart(toBody(it.item)),RequestType.ADD)
            is ShopIntent.getComments->httpRequest(apiService.getComments(it.id),RequestType.COMMENTS)

        }
    }

    /*fun getShops():Flow<PagingData<ShopBeanItem>>{
        return Pager(config = PagingConfig(pageSize = 10), pagingSourceFactory = {
            ShopDataSource(apiService,1)
        }).flow.cachedIn(viewModelScope)
    }*/
}